package github.saw96x.remoting.pojo;

import github.saw96x.common.enums.RpcResponseCodeEnum;
import lombok.*;

import java.io.Serializable;

/**
 * @author Saw96x
 * @date 2022/3/17 13:42
 */
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class RpcResponse<T> implements Serializable {

  private static final long serialVersionUID = 715745410605631233L;
  private String requestId;
  /**
   * Response结果码
   */
  private Integer code;
  /**
   * 响应消息
   */
  private String message;
  /**
   * response体
   */
  private T data;

  public static <T> RpcResponse<T> success(T data, String requestId) {
    RpcResponse<T> response = new RpcResponse<>();
    response.setCode(RpcResponseCodeEnum.SUCCESS.getCode());
    response.setMessage(RpcResponseCodeEnum.SUCCESS.getMessage());
    response.setRequestId(requestId);
    if (null != data) {
      response.setData(data);
    }
    return response;
  }

  public static <T> RpcResponse<T> fail(RpcResponseCodeEnum rpcResponseCodeEnum) {
    RpcResponse<T> response = new RpcResponse<>();
    response.setCode(rpcResponseCodeEnum.getCode());
    response.setMessage(rpcResponseCodeEnum.getMessage());
    return response;
  }

}
